clear;
phi_1 = 1/2 * [1 0 -1; 1 0 -1; 0 0 0; 0 0 0];
phi_2 = 1/3 * [1 1 1; 1 0 1; -1 -1 -1; 0 -1 0];
phi_3 = 1/3 * [0 1 0; 1 1 1; 1 0 1; 1 1 1];
phi_4 = 1/2 * [0 0 0; 0 0 0; 1 0 -1; 1 0 -1];

norm_1 = norm(phi_1, 'fro');
norm_2 = norm(phi_2, 'fro');
norm_3 = norm(phi_3, 'fro');
norm_4 = norm(phi_4, 'fro');

phi = {phi_1; phi_2; phi_3; phi_4};

num = numel(phi);

scalar_products = zeros(num, num);

for i = 1:num
    for j = 1:num
        if i == j
            continue; 
        end
        scalar_products(i, j) = dot(phi{i}(:), phi{j}(:));
    end
end

f = [-2 6 3; 13 7 5; 7 1 8; -3 4 4];
% 构造矩阵，列向量为基向量
A = [phi_1(:), phi_2(:), phi_3(:), phi_4(:)];
A

coefficients = inv(A'*A)*(A')*f(:);

% % 求解线性组合系数
% coefficients = A \ f(:);

% 重构向量
f_a = A * coefficients;

% 将重构的向量重新恢复为原始形状
f_a = reshape(f_a, size(f));

% 计算误差
difference = f - f_a;    % Calculate the element-wise difference
error = sum(sum(difference.^2));    % Calculate the squared Euclidean distance

coefficients
f_a
error